/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package vista;

import dao.DAOCliente;
import dao.DAOVentaInmueble;
import entidades.Cliente;
import entidades.Inmueble;
import entidades.ReservaInmueble;
import entidades.VentaInmueble;
import entidades.VentaInmuebleId;
import inmobiliariafrsf.GenerarVenta;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 * Interfaz para realizar la venta de un inmueble
 */
public class VVentaInmueble extends javax.swing.JDialog {

    /**
     * Inmueble sobre el que se va a realizar la venta
     */
    Inmueble inmuebleEnVenta;
    /**
     * Cliente que va a realizar la venta
     */
    Cliente clienteComprador;
    /**
     * Numero de documento del cliene
     */
    String numDoc_cliente;
    
    /**
     * Constructor de la interfaz
     * 
     * @param parent
     * @param modal
     * @param inmueble 
     */
    public VVentaInmueble(java.awt.Frame parent, boolean modal, Inmueble inmueble) {
        
        super(parent, modal);
        this.inmuebleEnVenta = inmueble;
        initComponents();
        
        if (this.inmuebleEnVenta != null){
            this.lCodigoInmueble.setText(this.inmuebleEnVenta.getCodigo());
            this.lTipoInmueble.setText(DAOCliente.getInmuebleTipoEnum(this.inmuebleEnVenta.getTipo()).toString());
            this.lFrenteInmueble.setText(Double.toString(this.inmuebleEnVenta.getFrente()) + " mts");
            this.lFondoInmueble.setText(Double.toString(this.inmuebleEnVenta.getFondo()) + " mts");
            this.lSuperficieInmueble.setText(Double.toString(this.inmuebleEnVenta.getSuperficie()) + " m2");
            this.lAntiguedadInmueble.setText(Integer.toString(this.inmuebleEnVenta.getAntiguedad()) + " años");
            this.lDormitoriosInmueble.setText(Byte.toString(this.inmuebleEnVenta.getDormitorios()));
            this.lBanosInmueble.setText(Byte.toString(this.inmuebleEnVenta.getBanos()));
            
            Vector<String> otrasCaracteristicas = new Vector<String>();
            if (this.inmuebleEnVenta.isGarage())
                otrasCaracteristicas.add("+Garage ");
            if (this.inmuebleEnVenta.isAguaCorriente())
                otrasCaracteristicas.add("+Agua Corriente ");
            if (this.inmuebleEnVenta.isAguaCaliente())
                otrasCaracteristicas.add("+Agua Caliente ");
            if (this.inmuebleEnVenta.isTelefono())
                otrasCaracteristicas.add("+Telefono ");
            if (this.inmuebleEnVenta.isPatio())
                otrasCaracteristicas.add("+Patio ");
            if (this.inmuebleEnVenta.isCloacas())
                otrasCaracteristicas.add("+Cloacas ");
            if (this.inmuebleEnVenta.isLavadero())
                otrasCaracteristicas.add("+Lavadero ");
            if (this.inmuebleEnVenta.isPiscina())
                otrasCaracteristicas.add("+Piscina ");
            if (this.inmuebleEnVenta.isGas())
                otrasCaracteristicas.add("+Gas ");
            if (this.inmuebleEnVenta.isPavimento())
                otrasCaracteristicas.add("+Pavimento ");
            
            int cant = otrasCaracteristicas.size();
            Iterator it = otrasCaracteristicas.iterator();

            String otras1 = "";
            String otras2 = "";
            
            if (0 == cant) 
                otras1 = "-";
            else {
                if (cant <= 5) {
                while (it.hasNext()) {
                    otras1 += it.next().toString();
                    }
                }
                else{
                    for (int i=0; i<5; i++){
                        otras1 += it.next().toString();
                    }
                    while (it.hasNext()) {
                        otras2 += it.next().toString();
                    }
                }
            }
            
            this.lOtrasInmueble1.setText(otras1);
            this.lOtrasInmueble2.setText(otras2);
            
            this.lProvinciaInmueble.setText(this.inmuebleEnVenta.getLocalidad().getProvincia().getNombre());
            this.lLocalidadInmueble.setText(this.inmuebleEnVenta.getLocalidad().getNombre());
            
            //Construccion del String de direccion del inmueble
            String direccion = this.inmuebleEnVenta.getCalle() + " " + Short.toString(this.inmuebleEnVenta.getNumero());
            if (this.inmuebleEnVenta.getPiso() == null)
                direccion += " " + Byte.toString(this.inmuebleEnVenta.getPiso()) + " " + this.inmuebleEnVenta.getDepartamento();
            
            this.lDomicilioInmueble.setText(direccion);
            
            this.lBarrio.setText(this.inmuebleEnVenta.getBarrio());
            this.lOrientacionInmueble.setText(this.inmuebleEnVenta.getOrientacion());
            
            this.tfMonto.setText("$" + Double.toString(this.inmuebleEnVenta.getPrecio()));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pInmueble = new javax.swing.JPanel();
        lProvincia = new javax.swing.JLabel();
        lLocalidad = new javax.swing.JLabel();
        lDomicilio = new javax.swing.JLabel();
        lBarrio = new javax.swing.JLabel();
        lOrientacion = new javax.swing.JLabel();
        lCodigo = new javax.swing.JLabel();
        lCodigoInmueble = new javax.swing.JLabel();
        lTipo = new javax.swing.JLabel();
        lTipoInmueble = new javax.swing.JLabel();
        lFrente = new javax.swing.JLabel();
        lFondo = new javax.swing.JLabel();
        lSuperficie = new javax.swing.JLabel();
        lFrenteInmueble = new javax.swing.JLabel();
        lFondoInmueble = new javax.swing.JLabel();
        lSuperficieInmueble = new javax.swing.JLabel();
        lAntiguedad = new javax.swing.JLabel();
        lDormitorios = new javax.swing.JLabel();
        lBanos = new javax.swing.JLabel();
        lOtras = new javax.swing.JLabel();
        lOtrasInmueble1 = new javax.swing.JLabel();
        lProvinciaInmueble = new javax.swing.JLabel();
        lLocalidadInmueble = new javax.swing.JLabel();
        lDomicilioInmueble = new javax.swing.JLabel();
        lBarrioInmueble = new javax.swing.JLabel();
        lOrientacionInmueble = new javax.swing.JLabel();
        lMonto = new javax.swing.JLabel();
        lAntiguedadInmueble = new javax.swing.JLabel();
        lDormitoriosInmueble = new javax.swing.JLabel();
        lBanosInmueble = new javax.swing.JLabel();
        tfMonto = new javax.swing.JTextField();
        lOtrasInmueble2 = new javax.swing.JLabel();
        pCliente = new javax.swing.JPanel();
        lNombreCliente = new javax.swing.JLabel();
        tfNombreCliente = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        bAceptar = new javax.swing.JButton();
        bCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(555, 427));
        setName("vVentaInmueble"); // NOI18N
        setResizable(false);

        pInmueble.setBorder(javax.swing.BorderFactory.createTitledBorder("Información Inmueble"));

        lProvincia.setText("Provincia:");

        lLocalidad.setText("Localidad:");

        lDomicilio.setText("Domicilio:");

        lBarrio.setText("Barrio:");

        lOrientacion.setText("Orientación:");

        lCodigo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lCodigo.setText("Código de Inmueble:");

        lCodigoInmueble.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lCodigoInmueble.setText("############");

        lTipo.setText("Tipo de Inmueble:");

        lTipoInmueble.setText("Departamento");

        lFrente.setText("Metros de Frente:");

        lFondo.setText("Metros de Fondo:");

        lSuperficie.setText("Superficie:");

        lFrenteInmueble.setText("####### mts");

        lFondoInmueble.setText("####### mts");

        lSuperficieInmueble.setText("####### mts");

        lAntiguedad.setText("Antigüedad:");

        lDormitorios.setText("Dormitorios:");

        lBanos.setText("Baños:");

        lOtras.setText("Otras características:");

        lOtrasInmueble1.setText("CARAAAAAAAAAAAAACTERISTICAS");

        lProvinciaInmueble.setText("SANTIAGO DEL ESTERO");

        lLocalidadInmueble.setText("SANTIAGO DEL ESTERO");

        lDomicilioInmueble.setText("SANTIAGO DEL ESTERO");

        lBarrioInmueble.setText("SANTIAGO DEL ESTERO");

        lOrientacionInmueble.setText("SANTIAGO DEL ESTERO");

        lMonto.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lMonto.setText("Monto:");

        lAntiguedadInmueble.setText("####### mts");

        lDormitoriosInmueble.setText("####### mts");

        lBanosInmueble.setText("####### mts");

        tfMonto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfMontoActionPerformed(evt);
            }
        });

        lOtrasInmueble2.setText("CARAAAAAAAAAAAAACTERISTICAS");

        javax.swing.GroupLayout pInmuebleLayout = new javax.swing.GroupLayout(pInmueble);
        pInmueble.setLayout(pInmuebleLayout);
        pInmuebleLayout.setHorizontalGroup(
            pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pInmuebleLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pInmuebleLayout.createSequentialGroup()
                        .addComponent(lCodigo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lCodigoInmueble))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pInmuebleLayout.createSequentialGroup()
                        .addComponent(lTipo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lTipoInmueble)
                        .addGap(36, 36, 36)))
                .addGap(143, 143, 143))
            .addGroup(pInmuebleLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pInmuebleLayout.createSequentialGroup()
                        .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lBarrio)
                            .addComponent(lProvincia)
                            .addComponent(lDomicilio))
                        .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pInmuebleLayout.createSequentialGroup()
                                .addGap(46, 46, 46)
                                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lFrenteInmueble)
                                    .addComponent(lAntiguedadInmueble))
                                .addGap(18, 18, 18)
                                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lDormitorios)
                                    .addComponent(lFondo))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(pInmuebleLayout.createSequentialGroup()
                                        .addComponent(lFondoInmueble)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addGroup(pInmuebleLayout.createSequentialGroup()
                                        .addComponent(lDormitoriosInmueble)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lBanos)
                                    .addComponent(lSuperficie))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lSuperficieInmueble)
                                    .addComponent(lBanosInmueble)))
                            .addGroup(pInmuebleLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lDomicilioInmueble, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(pInmuebleLayout.createSequentialGroup()
                                        .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addGroup(pInmuebleLayout.createSequentialGroup()
                                                .addComponent(lBarrioInmueble)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(lOrientacion))
                                            .addGroup(pInmuebleLayout.createSequentialGroup()
                                                .addComponent(lProvinciaInmueble)
                                                .addGap(52, 52, 52)
                                                .addComponent(lLocalidad)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lLocalidadInmueble, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addGroup(pInmuebleLayout.createSequentialGroup()
                                                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(lOrientacionInmueble)
                                                    .addComponent(lMonto))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(tfMonto, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))))))))
                    .addGroup(pInmuebleLayout.createSequentialGroup()
                        .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lFrente)
                            .addComponent(lAntiguedad))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(pInmuebleLayout.createSequentialGroup()
                        .addComponent(lOtras)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lOtrasInmueble2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lOtrasInmueble1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        pInmuebleLayout.setVerticalGroup(
            pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pInmuebleLayout.createSequentialGroup()
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lCodigo)
                    .addComponent(lCodigoInmueble))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lTipo)
                    .addComponent(lTipoInmueble))
                .addGap(18, 18, 18)
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lFrente)
                    .addComponent(lFondo)
                    .addComponent(lSuperficie)
                    .addComponent(lFrenteInmueble)
                    .addComponent(lFondoInmueble)
                    .addComponent(lSuperficieInmueble))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lAntiguedad)
                    .addComponent(lDormitorios)
                    .addComponent(lBanos)
                    .addComponent(lAntiguedadInmueble)
                    .addComponent(lDormitoriosInmueble)
                    .addComponent(lBanosInmueble))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lOtras)
                    .addComponent(lOtrasInmueble1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lOtrasInmueble2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lProvincia)
                    .addComponent(lProvinciaInmueble)
                    .addComponent(lLocalidad)
                    .addComponent(lLocalidadInmueble))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lDomicilio)
                    .addComponent(lDomicilioInmueble))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lBarrio)
                    .addComponent(lOrientacion)
                    .addComponent(lBarrioInmueble)
                    .addComponent(lOrientacionInmueble))
                .addGap(7, 7, 7)
                .addGroup(pInmuebleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lMonto)
                    .addComponent(tfMonto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pCliente.setBorder(javax.swing.BorderFactory.createTitledBorder("Cliente comprador"));

        lNombreCliente.setText("Nombre Completo:");

        tfNombreCliente.setEditable(false);
        tfNombreCliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tfNombreClienteMouseClicked(evt);
            }
        });

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/search_button_green_16.png"))); // NOI18N
        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pClienteLayout = new javax.swing.GroupLayout(pCliente);
        pCliente.setLayout(pClienteLayout);
        pClienteLayout.setHorizontalGroup(
            pClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pClienteLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lNombreCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tfNombreCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnBuscar))
        );
        pClienteLayout.setVerticalGroup(
            pClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pClienteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lNombreCliente)
                    .addComponent(tfNombreCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/disk.png"))); // NOI18N
        bAceptar.setText("Aceptar");
        bAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAceptarActionPerformed(evt);
            }
        });

        bCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/close_16.png"))); // NOI18N
        bCancelar.setText("Cancelar");
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(bAceptar)
                        .addGap(2, 2, 2)
                        .addComponent(bCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pInmueble, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 5, Short.MAX_VALUE))
                    .addComponent(pCliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pInmueble, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bCancelar)
                    .addComponent(bAceptar))
                .addContainerGap())
        );

        setSize(new java.awt.Dimension(571, 465));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void tfNombreClienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tfNombreClienteMouseClicked
    }//GEN-LAST:event_tfNombreClienteMouseClicked

    private void bAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAceptarActionPerformed
        if(this.clienteComprador!=null){
            if(!tfMonto.getText().isEmpty() || Integer.getInteger(tfMonto.getText()) > 0){
                VentaInmuebleId ventaId = new VentaInmuebleId(this.inmuebleEnVenta.getIdInmueble(), new Date());
                VentaInmueble venta = new VentaInmueble(ventaId, this.inmuebleEnVenta, VPrincipal.vLogeado, this.clienteComprador, this.inmuebleEnVenta.getPrecio());

                DAOVentaInmueble.insertOrUpdate(venta);

                //Generacion de reporte
                this.setVisible(false);

                //Se muestra una venta indicando que se está generando el reporte
                PopUp cargando = new PopUp(null, false);
                cargando.setVisible(true);

                new GenerarVenta(venta, this.lOtrasInmueble1.getText(), this.lOtrasInmueble2.getText());
                cargando.dispose();

                this.dispose();
            }
            else{
                JOptionPane.showMessageDialog(this, "Debe ingresar un monto para la venta", "Venta sin monto!", JOptionPane.WARNING_MESSAGE);    
            }
        }
        else{
            JOptionPane.showMessageDialog(this, "Debe seleccionar un cliente que realiza la compra", "Venta sin cliente!", JOptionPane.WARNING_MESSAGE);    
        }
        
    }//GEN-LAST:event_bAceptarActionPerformed

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_bCancelarActionPerformed

    public void setNumDocCliente(String numDoc_cliente) 
    {
        this.numDoc_cliente = numDoc_cliente;
        if(numDoc_cliente != null) {
            clienteComprador = DAOCliente.getByNumDoc(numDoc_cliente);
            
            if(clienteComprador != null) {
                // compruebo si el inmueble ya poseia reservas
                Set<ReservaInmueble> reservas = this.inmuebleEnVenta.getReservaInmuebles();
                if(!reservas.isEmpty()) {
                    Iterator it = reservas.iterator();
                    Date hoy = new Date();
                    Date fechaVencimiento;
                    Calendar vencimientoReserva = Calendar.getInstance();

                    while (it.hasNext()) {
                        ReservaInmueble res = (ReservaInmueble) it.next();
                        vencimientoReserva.setTime(res.getId().getFechaReserva());
                        vencimientoReserva.add(Calendar.DATE, res.getTiempoVigencia()+1); // le sumo 1 porque la reserva es hasta el ultimo día inclusive
                        fechaVencimiento = vencimientoReserva.getTime();

                        // compruebo que la reserva siga en vigencia
                        if (hoy.before(fechaVencimiento)) {
                            // verifico si el cliente seleccionado es el mismo que habia hecho la reserva
                            if (res.getCliente().getIdCliente() != this.clienteComprador.getIdCliente()) {
                                String nombreCliente = res.getCliente().getApellido()+", "+res.getCliente().getNombre();
                                JOptionPane.showMessageDialog(this, "El inmueble ha sido reservado por "+nombreCliente+" y no puede ser vendido a otro Cliente", "Reserva pendiente", JOptionPane.WARNING_MESSAGE);
                                this.numDoc_cliente = "";
                                this.clienteComprador = null;
                                return;
                            }
                        }
                    }
                }
                
                this.tfNombreCliente.setText(clienteComprador.getApellido()+", "+clienteComprador.getNombre());
            }
            else {
                this.tfNombreCliente.setText("Cliente no encontrado");
                JOptionPane.showMessageDialog(this, "El cliente no pudo Encontrarse", "Algo salió mal", JOptionPane.ERROR_MESSAGE);
            }
        }
        else {
            this.tfNombreCliente.setText("Cliente no seleccionado");
            JOptionPane.showMessageDialog(this, "El cliente no pudo Encontrarse", "Algo salió mal", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        VBuscarPersona vBc = new VBuscarPersona(this, "Cliente");
        vBc.setVisible(true);
    }//GEN-LAST:event_btnBuscarActionPerformed
  
    private void tfMontoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfMontoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfMontoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VVentaInmueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VVentaInmueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VVentaInmueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VVentaInmueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                VVentaInmueble dialog = new VVentaInmueble(new javax.swing.JFrame(),true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAceptar;
    private javax.swing.JButton bCancelar;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JLabel lAntiguedad;
    private javax.swing.JLabel lAntiguedadInmueble;
    private javax.swing.JLabel lBanos;
    private javax.swing.JLabel lBanosInmueble;
    private javax.swing.JLabel lBarrio;
    private javax.swing.JLabel lBarrioInmueble;
    private javax.swing.JLabel lCodigo;
    private javax.swing.JLabel lCodigoInmueble;
    private javax.swing.JLabel lDomicilio;
    private javax.swing.JLabel lDomicilioInmueble;
    private javax.swing.JLabel lDormitorios;
    private javax.swing.JLabel lDormitoriosInmueble;
    private javax.swing.JLabel lFondo;
    private javax.swing.JLabel lFondoInmueble;
    private javax.swing.JLabel lFrente;
    private javax.swing.JLabel lFrenteInmueble;
    private javax.swing.JLabel lLocalidad;
    private javax.swing.JLabel lLocalidadInmueble;
    private javax.swing.JLabel lMonto;
    private javax.swing.JLabel lNombreCliente;
    private javax.swing.JLabel lOrientacion;
    private javax.swing.JLabel lOrientacionInmueble;
    private javax.swing.JLabel lOtras;
    private javax.swing.JLabel lOtrasInmueble1;
    private javax.swing.JLabel lOtrasInmueble2;
    private javax.swing.JLabel lProvincia;
    private javax.swing.JLabel lProvinciaInmueble;
    private javax.swing.JLabel lSuperficie;
    private javax.swing.JLabel lSuperficieInmueble;
    private javax.swing.JLabel lTipo;
    private javax.swing.JLabel lTipoInmueble;
    private javax.swing.JPanel pCliente;
    private javax.swing.JPanel pInmueble;
    private javax.swing.JTextField tfMonto;
    private javax.swing.JTextField tfNombreCliente;
    // End of variables declaration//GEN-END:variables
}
